//
//  User.m
//  Courses
//
//  Created by xxx on 1/17/15.
//  Copyright (c) 2015 wtf. All rights reserved.
//

#import "User.h"



@implementation User
static NSString *USER_TOKEN_KEY = @"user_token";

- (id)initWithJsonDict:(NSDictionary *)jsonDict
{
    if (self = [super init])
    {
        self.userId = jsonDict[@"userId"];
        self.password = jsonDict[@"password"];
        self.userName = jsonDict[@"userName"];
        self.mobile = jsonDict[@"mobile"];
        self.loginStatus = jsonDict[@"loginStatus"];
        self.regTime = jsonDict[@"regTime"];
        self.roleId = jsonDict[@"roleId"];
        self.loginDate = jsonDict[@"loginDate"];
        self.agree = jsonDict[@"agree"];
        self.year = jsonDict[@"year"];
        self.month = jsonDict[@"month"];
        self.day = jsonDict[@"day"];
        self.hiidenMobile = jsonDict[@"hiidenMobile"];
        self.yearFromBirthday = jsonDict[@"yearFromBirthday"];
        self.monthFromBirthday = jsonDict[@"monthFromBirthday"];
        self.dayFromBirthday = jsonDict[@"dayFromBirthday"];
        self.objectId = jsonDict[@"objectId"];
    }
    return self;
}

+ (BOOL)isLoggedIn
{
    User *user = [User currentUser];
    return user.userId.length > 0;
}

+ (BOOL)signInWithMobile:(NSString *)mobile password:(NSString *)password
{
    Request *request = [Request requestWithName:@"login"];
    request.body[@"mobile"] = mobile;
    request.body[@"password"] = password;
    Response *response = [request response];
    [[NSUserDefaults standardUserDefaults]setObject:mobile forKey:@"currentMobile"];
    if (response.result)
    {
        [[NSUserDefaults standardUserDefaults] setObject:response.string forKey:USER_TOKEN_KEY];
        return true;
    }
    
    return false;
}

+ (BOOL)signUpWithMobile:(NSString *)mobile password:(NSString *)password verificationCode:(NSString *)verificationCode
{
    Request *request = [Request requestWithName:@"signup"];
    request.body[@"mobile"] = mobile;
    request.body[@"password"] = password;
    request.body[@"verificationCode"] = verificationCode;
    request.body[@"agencyId"] = @"YXUs";
    Response *response = [request response];
    if (response.result)
    {
        [[NSUserDefaults standardUserDefaults] setObject:response.string forKey:USER_TOKEN_KEY];
        return true;
    }
    
    return false;
}

+ (Response*)getVerificationCodeWithMobile:(NSString *)mobile
{
    Request *request = [Request requestWithName:@"get_verification_code"];
    request.body[@"mobile"] = mobile;
    return [request response];
}

+ (User *)currentUser
{
    NSString *jsonString = [[NSUserDefaults standardUserDefaults] objectForKey:USER_TOKEN_KEY];
    
    if (jsonString.length)
    {
        NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        User *user = [[self alloc] initWithJsonDict:json[@"msg"]];
//        user.mobile = [[NSUserDefaults standardUserDefaults]objectForKey:@"currentMobile"];
        NSLog(@"-----------%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"currentMobile"]);
        return user;
    }
    else
    {
        return nil;
    }
}
+ (void)setUserCancel{
    [[NSUserDefaults standardUserDefaults] setObject:NULL forKey: USER_TOKEN_KEY];
//    [[NSUserDefaults standardUserDefaults] setObject:NULL forKey:USER_TOKEN_KEY];
}

@end
